from toee import *

from Co8 import *

def san_dialog( attachee, triggerer ):
	if (triggerer.item_find(2203) != OBJ_HANDLE_NULL):
		triggerer.begin_dialog( attachee, 1 )
	elif (find_npc_near(attachee,8032) != OBJ_HANDLE_NULL):
		triggerer.begin_dialog( attachee, 100 )
	else:
		triggerer.begin_dialog( attachee, 130 )
	return SKIP_DEFAULT


def san_heartbeat( attachee, triggerer ):
	if (game.global_flags[361] == 0):
		game.global_flags[361] = 1
#################  Moved to Hedrack's summon_Iuz script by Cerulean the Blue:  It didn't work here, it does there.
		# game.particles( "mon-iuz", attachee )
	# nearby_pc = OBJ_HANDLE_NULL
	# test = 1
	# if test:
	# if (not game.combat_is_active()):
		# StopCombat(attachee)
		# for pc in game.obj_list_vicinity(attachee.location,OLC_PC):
			# if (is_safe_to_talk(attachee,pc)):
				# if (nearby_pc == OBJ_HANDLE_NULL):
					# nearby_pc = pc
				# if (pc.item_find(2203) != OBJ_HANDLE_NULL):
					# game.new_sid = 0
					# pc.begin_dialog(attachee,1)
					# return RUN_DEFAULT
		# if (nearby_pc != OBJ_HANDLE_NULL):
			# if (find_npc_near(attachee,8032) != OBJ_HANDLE_NULL):
				# game.new_sid = 0
				# nearby_pc.begin_dialog(attachee,100)
			# else:
				# game.new_sid = 0
				# nearby_pc.begin_dialog(attachee,130)
#################
	return RUN_DEFAULT

def san_dying( attachee, triggerer ):
	game.global_flags[327] = 1
	return RUN_DEFAULT


def san_start_combat( attachee, triggerer ):
	game.global_vars[32] = game.global_vars[32] + 1
	if (game.global_vars[32] >= 4):
		loc = attachee.location - 2
		cuthbert = game.obj_create( 14267, loc )
		StopCombat(attachee, 0)  # Added by Cerulean the Blue:  StopCombat is in Co8.py
		if (cuthbert != OBJ_HANDLE_NULL):
			for pc in game.obj_list_vicinity(cuthbert.location,OLC_PC):
				if (critter_is_unconscious(pc) == 0):
					pc.begin_dialog(cuthbert,1)
					game.new_sid = 0
					return SKIP_DEFAULT
## THIS IS USED FOR BREAK FREE
	for obj in game.party[0].group_list():
		if (obj.distance_to(attachee) <= 9 and obj.stat_level_get(stat_hp_current) >= -9):
			return RUN_DEFAULT		
	while(attachee.item_find(8903) != OBJ_HANDLE_NULL):
		attachee.item_find(8903).destroy()
	if (attachee.d20_query(Q_Is_BreakFree_Possible)):
		create_item_in_inventory( 8903, attachee )
##		attachee.d20_send_signal(S_BreakFree)
	return RUN_DEFAULT

def iuz_pc_persuade( iuz, pc, success, failure ):
	if (pc.saving_throw(10,D20_Save_Will,D20STD_F_NONE) == 0):
		pc.begin_dialog(iuz,failure)
	else:
		pc.begin_dialog(iuz,success)
	return SKIP_DEFAULT

def iuz_pc_charm( iuz, pc ):
	# auto dire charm the PC
	pc.dominate( iuz )
	if (game.party_pc_size() == 1):
		set_end_slides( iuz, pc )
		game.moviequeue_play_end_game()
	else:
		iuz.attack(pc)
	return SKIP_DEFAULT

def switch_to_hedrack( iuz, pc ):
	hedrack = find_npc_near(iuz,8032)
	if (hedrack != OBJ_HANDLE_NULL):
		pc.begin_dialog(hedrack,200)
		hedrack.turn_towards(iuz)
		iuz.turn_towards(hedrack)
	else:
		pc.begin_dialog(iuz,120)
	return SKIP_DEFAULT

def switch_to_cuthbert( iuz, pc, line ):
	cuthbert = find_npc_near(iuz,8043)
	if (cuthbert != OBJ_HANDLE_NULL):
		pc.begin_dialog(cuthbert,line)
		cuthbert.turn_towards(iuz)
		iuz.turn_towards(cuthbert)
	else:
		iuz.object_flag_set(OF_OFF)
	return SKIP_DEFAULT

def	find_ron( attachee, triggerer, line ):
	ron = find_npc_near(attachee,14681)
	cuthbert = find_npc_near(attachee,8043)
	if ((ron != OBJ_HANDLE_NULL) and (ron.distance_to(attachee) <= 12)):
		triggerer.begin_dialog(ron,line)
		ron.turn_towards(cuthbert)
	else:
		triggerer.begin_dialog(cuthbert,30)
		cuthbert.turn_towards(attachee)
		attachee.turn_towards(cuthbert)
	return SKIP_DEFAULT

def iuz_animate_troops( iuz, pc ):
	# animate evil creatures that are not PC's or PC followers and raise Hedrack
	npc = find_npc_near( iuz, 8032 ) # Hedrack
	if (npc.stat_level_get(stat_hp_current) <= -10):
		npc.resurrect( CRITTER_R_CUTHBERT_RESURRECT, 0 )
	else:
#		npc.obj_set_int( obj_f_hp_damage, 0 )
		dice = dice_new("1d10+1000")
		npc.heal( OBJ_HANDLE_NULL, dice )
		npc.healsubdual( OBJ_HANDLE_NULL, dice )
	npc.attack(pc)
	for npc in game.obj_list_vicinity(iuz.location,OLC_ALL):
		if (npc.stat_level_get(stat_hp_current) <= -10 and npc not in game.party and npc.name != 8032):
			iuz.cast_spell(spell_animate_dead, npc)
#			zombie = game.obj_create( 14123, npc.location )
#			game.particles( "sp-Iuz Making Zombies", zombie )
#			npc.destroy()
			return SKIP_DEFAULT
	return SKIP_DEFAULT


